package com.zlw.consumer;

import com.rabbitmq.client.Channel;
import com.zlw.pojo.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @author Zhaoluowei
 * @version 1.0
 * @date 2020/1/2 16:35
 */

@Slf4j
@Component
public class RabbitMQReceiver {

    @RabbitHandler
    @RabbitListener(
            bindings = @QueueBinding(value = @Queue(value = "queue-2", declare = "true"),
                    exchange = @Exchange(value = "exchange-2", type = "topic", declare = "true"),
                    key = "springboot.#"
            ))
    public void onMessage(Message message, Channel channel) {
        System.err.println("---------------------------------------------");
        System.err.println(" 消费端收到 Payload 消息体内容 --> " + message.getPayload());
        MessageHeaders headers = message.getHeaders();
        System.err.println(" 消费端收到 其他 消息内容 --> " + headers.toString());

        Long delivery_tag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        // 手工 ack
        try {
            channel.basicAck(delivery_tag, false);
        } catch (IOException e) {
            e.printStackTrace();
            log.info(" 记录异常信息 ");
        }

    }


    /**
     * 交换机是 direct
     *
     * @param message
     * @param channel
     */
    @RabbitHandler
    @RabbitListener(
            bindings = @QueueBinding(value = @Queue(value = "queue-3", declare = "true"),
                    exchange = @Exchange(value = "exchange-3", declare = "true"),
                    key = "springboot.abc.#"
            ))
    public void onMessage2(Message message, Channel channel) {
        System.err.println("---------------------------------------------");
        System.err.println("交换机是 direct 消费端收到 Payload 消息体内容 --> " + message.getPayload());
        MessageHeaders headers = message.getHeaders();
        System.err.println("交换机是 direct 消费端收到 其他 消息内容 --> " + headers.toString());

        Long delivery_tag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        // 手工 ack
        try {
            channel.basicAck(delivery_tag, false);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("交换机是 direct 记录异常信息");
        }

    }


    @RabbitHandler
    @RabbitListener(
            bindings = @QueueBinding(value = @Queue(value = "queue-4", declare = "true"),
                    exchange = @Exchange(value = "exchange-4", declare = "true"),
                    key = "springboot.abc.direct"
            ))
    public void onOderMessage(@Payload Order order, @Headers Map<String, Object> header,
                              Channel channel) {
        log.info("消费自定义消息体 order " + order);
        Long delivery_tag = (Long) header.get(AmqpHeaders.DELIVERY_TAG);
        log.info("消费自定义消息体  delivery_tag " + delivery_tag);

        // 手工 ack
        try {
            channel.basicAck(delivery_tag, false);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("消费自定义消息体  记录异常信息");
        }
    }


}
